package com.towker.core.util;

import org.apache.commons.lang.StringUtils;

public class StringUtil
{
	/**
	 * 判断字符串不为空串
	 * @param str
	 * @return String
	 */
	public static boolean isNotEmpty(String str)
	{
		boolean result = false;
		if (str != null)
		{
			str = str.trim();
			if (!"".equals(str))
				result = true;
		}
		return result;
	}

	/**
	 * html编码,[&=&amp; | <=&lt; | >=&gt; | "=&quot; | '=&apos;]
	 * @param str
	 * @return String
	 */
	public static String htmlEncode(String str)
	{
		if (str == null || str.length() == 0)
			return "";
		// 必须先转化&
		return str.replaceAll("&", "&amp;").replaceAll("", "&nbsp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
	}

	/**
	 * html解码,[&=&amp; | <=&lt; | >=&gt; | "=&quot; | '=&apos;]
	 * @param str
	 * @return String
	 */
	public static String htmlDecode(String str)
	{
		if (str == null || str.length() == 0)
			return "";
		// 必须最后转化&
//		return str.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;").replaceAll("&", "&amp;");
		return str.replaceAll("&nbsp;", "").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("'", "&apos;").replaceAll("&amp;", "&");
	}

	/**
	 * 对字符串escape编码
	 * @param str
	 * @return String
	 */
	public static String escape(String str)
	{
		if (str == null || str.length() == 0)
			return "";
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(str.length() * 6);
		for (i = 0; i < str.length(); i++)
		{
			j = str.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256)
			{
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			}
			else
			{
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

	/**
	 * 对字符串unescape解码
	 * @param str
	 * @return String
	 */
	public static String unescape(String str)
	{
		if (str == null || str.length() == 0)
			return "";
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(str.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < str.length())
		{
			pos = str.indexOf("%", lastPos);
			if (pos == lastPos)
			{
				if (str.charAt(pos + 1) == 'u')
				{
					ch = (char) Integer.parseInt(str.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				}
				else
				{
					ch = (char) Integer.parseInt(str.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			}
			else
			{
				if (pos == -1)
				{
					tmp.append(str.substring(lastPos));
					lastPos = str.length();
				}
				else
				{
					tmp.append(str.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}
	/**
	 * 将所传入的字符串转换为长整型
	 * 
	 * @param s
	 * @return Long
	 */
	public static Long toLong(String s) {
		if (!StringUtils.isBlank(s)) {
			return Long.valueOf(s.trim());
		}
		return null;
	}
	/**
	 * 将所传入的字符串转换为整型
	 * 
	 * @param s
	 * @return Integer
	 */
	public static Integer toInteger(String s) {
		if (!StringUtils.isBlank(s)) {
			return Integer.valueOf(s.trim());
		}
		return null;
	}
	/**
	 * 将所传入的字符串转换为双精度浮点型
	 * 
	 * @param s
	 * @return Double
	 */
	public static Double toDouble(String s) {
		if (!StringUtils.isBlank(s) && !"NaN".equals(s)) {
			return Double.valueOf(s.trim());
		}
		return 0D;
	}
}
